import telebot
import mysql.connector
# Подключение к базе данных MySQL
db_connection = mysql.connector.connect(
    host="localhost",
    user="vika",
    password="123123123",
    database="projectSV"
)
cursor = db_connection.cursor()
# Извлечение данных из таблицы
cursor.execute("WITH T AS (SELECT * FROM table1 WHERE date >= DATE_SUB(NOW(), INTERVAL 1 HOUR) ORDER BY name) SELECT T1.name, T1.id, T1.date, T1.came, T1.photo, T2.name, T2.id, T2.date, T2.came, TO_SECONDS(T2.date) - TO_SECONDS(T1.date) AS second FROM T AS T1, T AS T2 WHERE T1.id < T2.id AND T1.name = T2.name AND T2.id = (SELECT MIN(T.id) FROM T WHERE id > T1.id AND T.name = T1.name) AND TO_SECONDS(T2.date) - TO_SECONDS(T1.date)<15 AND T1.came<>T2.came;")
rows = cursor.fetchall()
print ("1")
# Отправка данных в телеграм бот
bot = telebot.TeleBot(token='6965078196:AAER3RCpiRZF686m-3NaY9euetHGDwUp9js')
chat_id = '5549629535'
for row in rows:
	# ID, name, photo = row[1], row[0], row[4]
	# message = f"ID: {ID}, Name: {name}, Value: {photo}"
	message = f"ID: {row[1]}, Name: {row[0]}"
	print ("2")
	bot.send_message(chat_id, message)
	photo = '/home/roman/Desktop/Face/facial_recognition/' + row[4]
	
	with open(photo, 'rb') as pic:
		bot.send_photo(chat_id, pic)
		#bot.send_photo(chat_id,urlopen(pic))
		print ("3")	
# Закрытие соединения с базой данных
cursor.close()
db_connection.close()
